package 单周赛.september;

/**
 * @
 * @date 2024/08/18
 */
public class 第411场单周赛 {

    public static void main(String[] args) {

        System.out.println(countKConstraintSubstrings("1010101", 2));
        System.out.println(countKConstraintSubstrings("10101", 1));

    }

    public static int countKConstraintSubstrings(String s, int k) {
        int ans = 0;
        int[] cnt = new int[2];
        int left = 0, right = 0;

        while (left < s.length()) {

            while (right < s.length()) {
                cnt[s.charAt(right) - '0']++;
                if (cnt[0] > k && cnt[1] > k) {
                    cnt[s.charAt(right) - '0']--;
                    break;
                }

                right++;
            }
            ans += right - left;

            cnt[s.charAt(left) - '0']--;
            left++;
        }

        return ans;
    }

}
